using System;
using System.Collections.Generic;
using System.Text;
using MeetingRoom.Data;
using MeetingRoom.Domain;
using MeetingRoom.Service;
using MeetingRoom.Service.Workflow.Implementation;
using NUnit.Framework;
using NMock;

namespace MeetingRoom.UnitTest
{
    [TestFixture]
    public class AvailablityCheckingPhaseTest
    {
        protected IGetSchedulesByMeetingRoomType mock;

        protected IGetMeetingRoomTypeBySize meetingRoomTypeQueryImpl;

        protected MeetingRoom.Domain.MeetingRoomType meetingRoomType;

        protected MeetingRoom.Domain.MeetingRoomType type2;

        protected Schedule request;

        protected Schedule schedule1, schedule2;

        protected List<Schedule> list;

        protected IMock meetingRoomTypeQuery;

        protected IMock getSchedulesByMeetingRoomTypeMockControl;

        protected IMock getMeetingRoomTypeMockControl;

        public AvailablityCheckingPhaseTest()
        {
        }

        [SetUp]
        public void SetUp()
        {
            meetingRoomType = new MeetingRoomType();
            meetingRoomType.Id = 1;
            meetingRoomType.Count = 2;
            meetingRoomType.Size = 20;

            type2 = new MeetingRoomType();
            type2.Count = 1;
            type2.Id = 1;
            type2.Size = 10;

            schedule1 = new Schedule();
            schedule1.Duration = new Duration(
                new DateTime(2007, 1, 10, 1, 0, 0), new DateTime(2007, 1, 12, 3, 0, 0));
            schedule1.MeetingRoomType = meetingRoomType;

            schedule2 = new Schedule();
            schedule2.Duration = new Duration(
                new DateTime(2007, 1, 13, 1, 0, 0), new DateTime(2007, 1, 14, 3, 0, 0));
            schedule2.MeetingRoomType = type2;

            this.getMeetingRoomTypeMockControl = new DynamicMock(
                typeof(IGetMeetingRoomTypes));
            this.getMeetingRoomTypeMockControl.ExpectAndReturn(
                "GetMeetingRoomTypes",
                new MeetingRoomType[] { type2, meetingRoomType }, this.meetingRoomType);

            this.getSchedulesByMeetingRoomTypeMockControl = new DynamicMock(
                typeof(IGetSchedulesByMeetingRoomType));
            this.getSchedulesByMeetingRoomTypeMockControl.ExpectAndReturn(
                "Query", new Schedule[] { schedule2 }, type2);
            this.getSchedulesByMeetingRoomTypeMockControl.ExpectAndReturn(
                "Query", new Schedule[] { schedule1 }, meetingRoomType);

            request = new Schedule();
            request.Duration = new Duration(
                new DateTime(2007, 1, 13, 2, 0, 0), new DateTime(2007, 1, 14, 4, 0, 0));
            request.Name = "Hello";
            request.MeetingRoomType = meetingRoomType;
        }

        [TearDown]
        public void TearDown()
        {
            this.getSchedulesByMeetingRoomTypeMockControl.Verify();
            this.getMeetingRoomTypeMockControl.Verify();
        }

        [Test]
        public void TestUnavailable()
        {
            meetingRoomType.Count = 1;
            AvailablityCheckingPhase phase = new AvailablityCheckingPhase();
            phase.GetMeetingRoomTypeImplementor =
                (IGetMeetingRoomTypes)this.getMeetingRoomTypeMockControl.MockInstance;
            phase.GetSchedulesByMeetingRoomTypeImplementor =
                (IGetSchedulesByMeetingRoomType)
                getSchedulesByMeetingRoomTypeMockControl.MockInstance;

            try
            {
                phase.Execute(request);
                Assert.IsTrue(false);
            }
            catch (MeetingRoomUnavaliableException ex)
            {
                Assert.IsTrue(true);
            }
        }

        [Test]
        public void TestAvailable()
        {
            AvailablityCheckingPhase phase = new AvailablityCheckingPhase();
            phase.GetMeetingRoomTypeImplementor = (IGetMeetingRoomTypes)
                this.getMeetingRoomTypeMockControl.MockInstance;
            phase.GetSchedulesByMeetingRoomTypeImplementor =
                (IGetSchedulesByMeetingRoomType)
                getSchedulesByMeetingRoomTypeMockControl.MockInstance;

            try
            {
                phase.Execute(request);
                Assert.IsTrue(true);
            }
            catch
            {
                Assert.IsTrue(false);
            }
        }
    }
}
